package com.java8.demo02_basic_use;

import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by liuburu on 2017/6/8.
 */
public class HelloLambdaTest {

    /**
     * 测试1：传统比较字符串方式
     */
    @Test
    public void test1() {
        List<String> names = Arrays.asList("peter", "anna", "mike", "xenia");
        Collections.sort(names, new Comparator<String>() {
            @Override
            public int compare(String a, String b) {
                return a.compareTo(b);
            }
        });
        System.out.println(names);
    }

    /**
     * 测试2：使用Lambda表达式
     */
    @Test
    public void test2() {
        List<String> wordList = Arrays.asList("dog", "apple", "banana", "chicken", "football", "egg", "gold");

        // 标准lambda
        Collections.sort(wordList, (String a, String b) -> {
            return a.compareTo(b);
        });

        // 省略方法体“{}”,省略return关键字
        Collections.shuffle(wordList);
        Collections.sort(wordList, (String a, String b) -> a.compareTo(b));

        // 省略方法参数
        Collections.shuffle(wordList);
        Collections.sort(wordList, (a, b) -> a.compareTo(b));

        // 直接使用静态方法
        Collections.shuffle(wordList);
        /**
         *  ☆☆☆推荐这种使用方式☆☆☆
         *  说明：`String::compareTo`可以这么使用是因为Comparator可以使用函数式编程，所以可以直接调用`String::CompareTo`
         *  @FunctionalInterface public interface Comparator<T> {}
         */
        Comparator<String> stringComparator = String::compareTo;
        Collections.sort(wordList, stringComparator);

        // 使用Comparator.naturalOrder()进行排序
        Collections.shuffle(wordList);
        Comparator<String> naturalOrderComparator = Comparator.naturalOrder();
        Collections.sort(wordList, naturalOrderComparator);

    }


    /**
     * 拓展训练: 使用Lambda表达式，灵活的替代内部类
     */

    // 鸟说话接口
    @FunctionalInterface
    interface IBirdCapacity {
        void speak(String word);
    }

    // 动物创建接口
    @FunctionalInterface
    interface BirdFactory<T extends Bird> {
        Bird create();
    }

    // 所有鸟的父类
    class Bird {
    }

    /**
     * 鹦鹉类
     */
    class Parrot extends Bird implements IBirdCapacity {
        @Override
        public void speak(String word) {
            System.out.println("鹦鹉:" + word);
        }
    }

    /**
     * 人类
     */
    class Person {
        private String name;

        public Person(String name) {
            this.name = name;
        }
        public void letBirdSayWord(IBirdCapacity IBirdCapacity) {
            System.out.println(name + "让他的鸟说话。");
            IBirdCapacity.speak(name);
        }
    }

    @Test
    public void test4() {
        Person person = new Person("kiwipeach");
        // 1.传统方式
        person.letBirdSayWord(new IBirdCapacity() {
            @Override
            public void speak(String word) {
                System.out.println("黄鹂鸟:" + word);
            }
        });

        // 2.java8中的lambda表达式
        person.letBirdSayWord((word) -> {
            System.out.println("白鹭:" + word);
        });

        // 3.函数式接口（函数式创建对象）
        BirdFactory<Parrot> parrotBirdFactory = Parrot::new;
        Parrot parrot = (Parrot) parrotBirdFactory.create();
        person.letBirdSayWord(parrot);

        /**
         * kiwipeach让他的鸟说话。
         * 黄鹂鸟:kiwipeach
         * kiwipeach让他的鸟说话。
         * 白鹭:kiwipeach
         * kiwipeach让他的鸟说话。
         * 鹦鹉:kiwipeach
         */
    }
}
